﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Security;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace CloudSharingStation_UWP_.Model
{
    class User : Account
    {
        public string departmentName;

        public object JsonSerializer { get; private set; }

        static public User user { get; set; } // 静态单例对象

        private User(string name,string password,string setDeparmentName) : base(name, password)
        {
            this.name = name;
            this.password = password;
            departmentName = setDeparmentName;
        }
        static public void SetUser(string name,string password,string derpatmentName="")
        {
            if(user == null) { 
            user = new User(name, password, derpatmentName); // 创建单例对象
            }
            else
            {
                // 如果用户以及存在则修改用户的属性
                user.name = name;
                user.password = password;
                user.departmentName = derpatmentName;
            }
        }

        /// <summary> 发送注册账户的Http Post请求
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="confirmPassword"></param>
        /// <param name="departmentName"></param>
        public Dictionary<HttpStatusCode, string> RegisterAsync()
        {

            Dictionary<HttpStatusCode, string> resDic = new Dictionary<HttpStatusCode, string>();
            var jsonParameters = new Dictionary<string, object>
               {
                 {"name", user.name }, {"password", user.password},{"department",user.departmentName}
               };
            string jsonData = JsonConvert.SerializeObject(jsonParameters);
            try
            {
                var apiUrl = "https://localhost:" + Config.localNum + "/api/user/register";
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(Config.CheckValidationResult);
                var req = (HttpWebRequest)WebRequest.Create(apiUrl);
                req.Method = "POST";
                req.ContentType = "application/json"; // 以json格式传送数据
                req.ProtocolVersion = HttpVersion.Version10;
                req.UserAgent = Config.DefaultUserAgent;

                // 如果json数据不是null，也非空则写入请求中
                if (!string.IsNullOrEmpty(jsonData))
                {
                    using (var postStream = new StreamWriter(req.GetRequestStream()))
                    {
                        postStream.Write(jsonData);
                    }
                }

                var response = (HttpWebResponse)req.GetResponse();
                string result = ""; // 存储结果的字符串
                Stream stream = response.GetResponseStream();
                try
                {
                    //获取内容
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        result = reader.ReadToEnd();
                    }
                }
                finally
                {
                    stream.Close();
                }
                resDic.Add(response.StatusCode, result); // 将服务器响应的状态码和结果内容加入到字典中
                return resDic; // 返回结果字典

            }
            catch (Exception ex)
            {
                // Write out any exceptions.
                Debug.WriteLine(ex);
                Dictionary<HttpStatusCode, string> exceptionDic = new Dictionary<HttpStatusCode, string>();
                exceptionDic.Add(HttpStatusCode.BadRequest, "注册异常");
                return exceptionDic;
            }
            finally
            {
                user = null; // 重新销毁对象
            }

        }
        public void Comment()
        {
            //评论相关的功能本次大作业不做实现
        }

    }
}
        

